package com.ctvit.breadking.news.website.service.impl;

import com.ctvit.breadking.news.core.constant.CommonConstant;
import com.ctvit.breadking.news.core.solr.config.SolrConfig;
import com.ctvit.breadking.news.core.solr.repository.SolrDocumentRepository;
import com.ctvit.breadking.news.core.solr.utils.SolrQueryUtils;
import com.ctvit.breadking.news.website.pojo.entity.WebsiteEntity;
import com.ctvit.breadking.news.website.pojo.dto.WebsiteDTO;
import com.ctvit.breadking.news.website.pojo.vo.WebsiteVO;
import com.ctvit.breadking.news.website.service.WebsiteService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.LBHttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.ctvit.breadking.news.core.solr.utils.SolrQueryUtils.SOLR_QUERY_ALL;

@Service
public class WebsiteServiceImpl implements WebsiteService {

    private final SolrConfig solrConfig;

    private final SolrDocumentRepository solrDocumentRepository;

    public WebsiteServiceImpl(SolrConfig solrConfig, SolrDocumentRepository solrDocumentRepository) {
        this.solrConfig = solrConfig;
        this.solrDocumentRepository = solrDocumentRepository;
    }

    @Override
    public boolean save(WebsiteDTO websiteDTO) {
        SolrInputDocument websiteDocument = new SolrInputDocument();
        websiteDocument.addField(WebsiteEntity.FIELD_CODE, websiteDTO.getCode());
        websiteDocument.addField(WebsiteEntity.FIELD_NAME, websiteDTO.getName());
        websiteDocument.addField(WebsiteEntity.FIELD_URL, websiteDTO.getUrl());
        websiteDocument.addField(CommonConstant.FIELD_CREATE_TIME, new Date());

        return solrDocumentRepository.add(WebsiteEntity.COLLECTION_WEBSITE, websiteDTO.getCode(), websiteDocument);
    }

    @Override
    public boolean delete(String code) {
        return solrDocumentRepository.deleteById(WebsiteEntity.COLLECTION_WEBSITE, code);
    }

    @Override
    public List<WebsiteVO> list() {
        List<WebsiteVO> records = Collections.emptyList();

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(SOLR_QUERY_ALL);
        solrQuery.addSort(WebsiteEntity.FIELD_CODE, SolrQuery.ORDER.asc);

        LBHttpSolrClient lbHttpSolrClient = solrConfig.buildLbHttpSolrClient(WebsiteEntity.COLLECTION_WEBSITE);
        QueryResponse response = solrDocumentRepository.query(lbHttpSolrClient, solrQuery);
        SolrDocumentList queryResults = response.getResults();
        if (CollectionUtils.isNotEmpty(queryResults)) {
            records = new ArrayList<>(queryResults.size());

            for (SolrDocument queryResult : queryResults) {
                WebsiteVO websiteVO = assignWebsiteVO(queryResult);
                records.add(websiteVO);
            }
        }
        return records;
    }

    private WebsiteVO assignWebsiteVO(SolrDocument solrDocument) {
        WebsiteVO websiteVO = new WebsiteVO();
        websiteVO.setCode(SolrQueryUtils.obtainStringValue(solrDocument, WebsiteEntity.FIELD_CODE));
        websiteVO.setName(SolrQueryUtils.obtainStringValue(solrDocument, WebsiteEntity.FIELD_NAME));
        websiteVO.setUrl(SolrQueryUtils.obtainStringValue(solrDocument, WebsiteEntity.FIELD_URL));
        websiteVO.setCreateTime(SolrQueryUtils.obtainDateValue(solrDocument, CommonConstant.FIELD_CREATE_TIME));
        return websiteVO;
    }

}
